package LeetCode;

class Solution2 {
    /**
     * 202. 快乐数
     * https://leetcode.cn/problems/happy-number/description/
     * @param n
     * @return
     */
    public boolean isHappy(int n) {
        int slow = 0, fast = 1;
        int s = n, f = n;
        while(slow != fast) {
            int count1 = 0;
            int count2 = 0;
            while(s > 0) {
                int tmp = s % 10;
                s /= 10;
                count1 += tmp * tmp;
            }
            slow = count1;
            s = count1;
            while(f > 0) {
                int tmp = f % 10;
                f /= 10;
                count2 += tmp * tmp;
            }
            f = count2;
            count2 = 0;
            while(f > 0) {
                int tmp = f % 10;
                f /= 10;
                count2 += tmp * tmp;
            }
            fast = count2;
            f = count2;
        }
        return slow == 1;
    }
}